package com.mantru.iotpot.Dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.mantru.iotpot.Adapter.AddStepDialogAdapter;
import com.mantru.iotpot.Bean.AddStepDialogBean;
import com.mantru.iotpot.R;

import java.util.ArrayList;
import java.util.List;

public class AddStepDialog extends Dialog {
    Activity context;
    private AddStepDialogAdapter addStepDialogAdapter;
    private ConstraintLayout heat;
    private TextView heatText;
    private ConstraintLayout sauce;
    private TextView sauceText;
    private ConstraintLayout food;
    private TextView foodText;
    private RecyclerView recyclerView;
    private TextView confirm;
    private ImageView close;
    private List list;
    private String type = "";
    private int positionSend = -1;

    private List<AddStepDialogBean> addStepDialogBeans = new ArrayList<>();
    private List<AddStepDialogBean> addStepDialogList = new ArrayList<>();
    private AddStepDialogListener addStepDialogListener;
    public void setAddStepDialogListener(AddStepDialogListener addStepDialogListener){
        this.addStepDialogListener = addStepDialogListener;
    }
    public interface AddStepDialogListener{
        void confirm(String type, int position);
    }

    public AddStepDialog(Activity context, int theme, List list){
        super(context, theme);
        this.context = context;
        this.list = list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.dialog_add_step);

        initView();//set my control here
        setDialogCancelable();//set dialog cancelable or not
        //初始化recyclerview
        setRecyclerView();
        //设置adapter数据
        putData();
        setDialogListener();
    }

    //set my control here
    private void initView(){
        heat = findViewById(R.id.dialog_add_heat);
        heatText = findViewById(R.id.dialog_add_heat_text);
        sauce = findViewById(R.id.dialog_add_sauce);
        sauceText = findViewById(R.id.dialog_add_sauce_text);
        food = findViewById(R.id.dialog_add_food);
        foodText = findViewById(R.id.dialog_add_food_text);
        recyclerView = findViewById(R.id.dialog_add_step_recyclerView);
        confirm = findViewById(R.id.dialog_add_step_confirm);
        close = findViewById(R.id.dialog_add_close);
    }

    private void setDialogCancelable(){
        this.setCancelable(true);
    }

    //初始化recyclerview
    private void setRecyclerView(){
        addStepDialogAdapter = new AddStepDialogAdapter(context, R.layout.adapter_add_step_dialog, addStepDialogList);

        // 配置RecyclerView
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
        layoutManager.setFlexDirection(FlexDirection.ROW);   // 水平方向
        layoutManager.setFlexWrap(FlexWrap.WRAP);            // 允许换行
        layoutManager.setAlignItems(AlignItems.STRETCH);     // 拉伸对齐

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(addStepDialogAdapter);
    }

    @SuppressLint("NotifyDataSetChanged")
    private void putData(){
        addStepDialogBeans = getAddStepDialogBeansList();

        if ( addStepDialogBeans.isEmpty()) {
            addStepDialogList.clear();
        }
        addStepDialogAdapter.notifyDataSetChanged();
    }

    private List<AddStepDialogBean> getAddStepDialogBeansList(){
        try {
            List<AddStepDialogBean> addStepDialogBeans = new ArrayList<>();
            for (int i = 0; i < list.size(); i++ ) {
                AddStepDialogBean addStepDialogBean = new AddStepDialogBean(
                        String.valueOf(i + 1),
                        false
                );
                addStepDialogBeans.add(addStepDialogBean);
            }
            addStepDialogList.clear();
            addStepDialogList.addAll(addStepDialogBeans);
            return addStepDialogBeans;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private void setDialogListener(){
        heat.setOnClickListener(v -> {
            type = "加热设置";
            heat.setBackgroundResource(R.drawable.radius8_1151e4);
            sauce.setBackgroundResource(R.drawable.radius8_line_a1a1a7);
            food.setBackgroundResource(R.drawable.radius8_line_a1a1a7);
            heatText.setTextColor(Color.parseColor("#FFFFFF"));
            sauceText.setTextColor(Color.parseColor("#000000"));
            foodText.setTextColor(Color.parseColor("#000000"));
        });

        sauce.setOnClickListener(v -> {
            type = "调味料";
            heat.setBackgroundResource(R.drawable.radius8_line_a1a1a7);
            sauce.setBackgroundResource(R.drawable.radius8_1151e4);
            food.setBackgroundResource(R.drawable.radius8_line_a1a1a7);
            heatText.setTextColor(Color.parseColor("#000000"));
            sauceText.setTextColor(Color.parseColor("#FFFFFF"));
            foodText.setTextColor(Color.parseColor("#000000"));
        });

        food.setOnClickListener(v -> {
            int num = 0;
            for ( int i = 0; i < list.size(); i++ ){
                if ( list.get(i).equals("3") ){
                    num++;
                }
            }
            if ( num >= 4 ){
                Toast.makeText(context, "最多只有4个料盒，无法插入", Toast.LENGTH_SHORT).show();
            } else {
                type = "固体料盒";
                heat.setBackgroundResource(R.drawable.radius8_line_a1a1a7);
                sauce.setBackgroundResource(R.drawable.radius8_line_a1a1a7);
                food.setBackgroundResource(R.drawable.radius8_1151e4);
                heatText.setTextColor(Color.parseColor("#000000"));
                sauceText.setTextColor(Color.parseColor("#000000"));
                foodText.setTextColor(Color.parseColor("#FFFFFF"));
            }
        });

        addStepDialogAdapter.setAddStepListener((position, isSelected) -> {
            if ( isSelected ){
                positionSend = position;
            }
            addStepDialogAdapter.notifyDataSetChanged();
        });

        confirm.setOnClickListener(v -> {
            if ( type.isEmpty() ){
                Toast.makeText(context, "请选择插入类型", Toast.LENGTH_SHORT).show();
                return;
            }
            if ( positionSend == -1 ){
                Toast.makeText(context, "请选择插入位置", Toast.LENGTH_SHORT).show();
                return;
            }
            addStepDialogListener.confirm(type, positionSend);
            dismiss();
        });

        close.setOnClickListener(v -> dismiss());
    }
}
